<template>
  <p-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">
        <p-row>
		  <p-col  :span="24">
			<p-form-item label="培训单号" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input readOnly v-decorator="[ 'trainBillno', validatorRules.trainBillno]" placeholder="请输入培训单号"></p-input>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="员工类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<j-dict-select-tag  type="list" v-decorator="['empType', validatorRules.empType]" :trigger-change="true" dictCode="mes_emp_type" placeholder="请选择员工类型"/>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="计划开始日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<j-date    placeholder="请选择计划开始日期" v-decorator="[ 'planStartDate', validatorRules.planStartDate]" :trigger-change="true" style="width: 100%"/>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="计划结束日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<j-date    placeholder="请选择计划结束日期" v-decorator="[ 'planEndDate', validatorRules.planEndDate]" :trigger-change="true" style="width: 100%"/>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="计划培训人数" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input-number   v-decorator="[ 'planTrainMembers', validatorRules.planTrainMembers]" placeholder="请输入计划培训人数" style="width: 100%"/>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="已完成人数" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input-number   v-decorator="[ 'finishMembers', validatorRules.finishMembers]" placeholder="请输入已完成人数" style="width: 100%"/>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="完成情况" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'finishCondition', validatorRules.finishCondition]" placeholder="请输入完成情况"></p-input>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'remark', validatorRules.remark]" placeholder="请输入备注"></p-input>
			</p-form-item>
		  </p-col>
        </p-row>


      </p-form>
    </p-spin>
  </p-modal>
</template>

<script>


  import { SubmitOnchangMixin } from '@/mixins/SubmitOnchangMixin'
  import { httpAction, putAction, api } from '@/api/manage'
  import pick from 'lodash.pick'
  import JDate from '@/components/jeecg/JDate'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"

  export default {
    name: "MesHrMemberTrainPlanModal",
	mixins: [SubmitOnchangMixin],
    components: { 
      JDate,
      JDictSelectTag,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title: '操作',
        width: 800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules:{
        trainBillno:{},
        empType:{rules: [{ required: true, message: '请输入员工类型!' }]},
        planStartDate:{rules: [{ required: true, message: '请输入计划开始日期!' }]},
        planEndDate:{rules: [{ required: true, message: '请输入计划结束日期!' }]},
        planTrainMembers:{rules: [{ required: true, message: '请输入计划培训人数!' }]},
        finishMembers:{},
        finishCondition:{},
        remark:{},
        },
        url: {
          add: '/mes-manpower/auto/mesHrMemberTrainPlan/add',
          edit: '/mes-manpower/auto/mesHrMemberTrainPlan/edit',
        }
     
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
        this.getRuleCodeValue();
      },
      getRuleCodeValue() {
        let param = {
          commonFormData: this.model,
          rules: [
            {"ruleCode": "mes_train_billno", "columnName": "trainBillno"},
            ]
        }

        putAction(api.ruleBaseURL, param).then(res => {
          // 执行成功，获取返回的值，并赋到页面上
          if (res.success) {
            console.log("生成的编码：" + res.data)
            const item = res.data;
            this.form.setFieldsValue(pick(item, Object.keys(item)))
          }
        })
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'trainBillno','empType','planStartDate','planEndDate','planTrainMembers','finishMembers','finishCondition','createBy','createTime','updateBy','updateTime','remark'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
		handleOk () {
			const that = this;
			// 触发表单验证
			this.form.validateFields((err, values) => {
			  if (!err) {
				  that.confirmLoading = true;
				  let httpurl = '';
				  let method = '';
				  let formData = Object.assign(this.model, values);
				  this.formData = Object.assign(this.model, values);
				  //新增触发
				  if(!this.model.id){
					httpurl+=this.url.add;
					method = 'post';
					if(this.EnhanceJS.afterAdd){
					  //新增之前
					  let Obj = eval(this.EnhanceJS.afterAdd)
					  new Obj(this)
					}else{
					  console.log("表单提交数据",formData)
					  that.modalFrom(httpurl,formData,method)
					}
				  }else{
					//编辑触发
					httpurl+=this.url.edit;
					method = 'put';
					if(this.EnhanceJS.afterEdit){
					 //编辑之前
					  let Obj = eval(this.EnhanceJS.afterEdit)
					  new Obj(this)
					}else{
					  console.log("表单提交数据",formData)
					  that.modalFrom(httpurl,formData,method)
					}
				  }
			  }

			})
		},
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'trainBillno','empType','planStartDate','planEndDate','planTrainMembers','finishMembers','finishCondition','createBy','createTime','updateBy','updateTime','remark'))
      },

      
    }
  }
</script>